package nancal.mp.mvc.sys

import nancal.mp.mvc.util.MyEmail
import nancal.mp.mvc.util.MyEmail.SMTP_ENT_QQ
import nancal.mp.service.MyEmailService
import nbcp.base.comm.JsonResult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.annotation.PostConstruct

/**
 * @Author: Cuiyong
 * @Date: 2021/9/3 11:33
 */
@RestController
@RequestMapping("/mail")
class SendMailController {
    @Autowired
    private val myEmailService: MyEmailService? = null

    class MailVo {
        val title: String= ""
        val from: String= ""
        val html: String= ""
        val text: String= ""
        val to: String = ""
        //附件列表 网络附件  key=文件名  value=文件url地址
        val attachUrlMap: HashMap<String, String>? = null
    }

    @PostConstruct
    fun init() {
        // 配置，一次即可
        MyEmail.config(SMTP_ENT_QQ(false), "cuiyong@nancal.com", "aAa1027910910")
    }

    @PostMapping("/sendText")
    fun sendText(
            @RequestBody vo: MailVo
    ): JsonResult {
        return try {
            myEmailService!!.sendTextMail(vo)
        } catch (e: Exception) {
            return JsonResult.error("发送邮件错误:$e")
        }
    }

    @PostMapping("/sendHtml")
    fun sendHtml(
            @RequestBody vo: MailVo
    ): JsonResult {
        return try {
            myEmailService!!.sendHtml(vo)
        } catch (e: Exception) {
            return JsonResult.error("发送邮件错误:$e")
        }
    }
}